import 'package:flutter/material.dart';

import '../resources.dart';

/// created by hcl
/// on 2024/4/5
/// page MarkLightText
/// desc 文字标签和名称混排
class MarkLightText extends StatelessWidget {
  final String markText;
  final Color markColor;
  final String lightText;
  final String content;
  final int maxLines;
  final TextStyle? contentStyle;
  final TextStyle? lightStyle;
  final FontWeight weight;

  const MarkLightText(this.content, this.lightText,
      {super.key,
      this.markText = '',
      this.markColor = ColorUtils.app_color,
      this.maxLines = 1,
      this.contentStyle,
      this.lightStyle,
      this.weight = FontWeight.bold});

  @override
  Widget build(BuildContext context) {
    List<InlineSpan> spanList = [];
    if (markText.isNotEmpty) {
      spanList.add(WidgetSpan(
        child: Container(
          constraints: BoxConstraints(maxWidth: getScreenSize(100)),
          padding: EdgeInsets.only(
              left: getScreenSize(6),
              right: getScreenSize(6),
              top: getScreenSize(0),
              bottom: getScreenSize(1)),
          margin: EdgeInsets.only(right: getScreenSize(4)),
          decoration: BoxDecoration(
              color: markColor.withOpacity(0.1),
              borderRadius: BorderRadius.circular(getScreenSize(4))),
          child: MediaQuery(
            data: const MediaQueryData(textScaler: TextScaler.linear(1)),
            child: Text(
              markText,
              style: TextStyle(
                  color: markColor, fontSize: 12, fontWeight: FontWeight.w600),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ),
      ));
    }
    spanList.addAll(getLightSpan(content, lightText, contentStyle ?? medium314,
        lightStyle ?? medium3C14));

    return Text.rich(
      TextSpan(children: spanList),
      maxLines: maxLines,
      overflow: TextOverflow.ellipsis,
    );
  }

  List<InlineSpan> getLightSpan(
    String content,
    String lightContent,
    TextStyle contentStyle,
    TextStyle lightStyle,
  ) {
    List<InlineSpan> spans = [];
    if (content.isNotEmpty) {
      if (lightContent.isNotEmpty && content.contains(lightContent)) {
        var indexOf = content.indexOf(lightContent);

        ///如果大于0 添加高亮前置位的文字
        if (indexOf > 0) {
          spans.add(
            TextSpan(
                text: content.substring(0, indexOf).replaceAll("", "\u200B"),
                style: contentStyle),
          );
        }
        spans.add(TextSpan(
            text: lightContent.replaceAll("", "\u200B"), style: lightStyle));
        spans.add(
          TextSpan(
              text: content
                  .substring(indexOf + lightContent.length)
                  .replaceAll("", "\u200B"),
              style: contentStyle),
        );
      } else {
        spans.add(TextSpan(
            text: content.replaceAll("", "\u200B"), style: contentStyle));
      }
    }

    return spans;
  }
}
